package converters;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import repositories.VecinoRepository;
import domain.Vecino;

@Component
@Transactional
public class StringToVecinoConverter implements Converter<String, Vecino> {

	@Autowired
	VecinoRepository vecinoRepository;

	public Vecino convert(String text) {
		Vecino result;
		int id;

		try {
			id = Integer.valueOf(text);
			result = vecinoRepository.findOne(id);
		} catch (Throwable oops) {
			throw new IllegalArgumentException(oops);
		}

		return result;
	}
}
